package nlzx;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Cleanup;
import org.apache.poi.ss.usermodel.*;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class Excel2Json {
    public static void main(String[] args){
        try {
            Properties properties = new Properties();

            FileInputStream properitesInputStream = new FileInputStream("E:/一张网/环境/NLZX/nlzxConfig.properties");
            InputStreamReader reader = new InputStreamReader(properitesInputStream, StandardCharsets.UTF_8);
            properties.load(reader);
            properitesInputStream.close();

            String importPath = properties.getProperty("importPath");
            String exportPath = properties.getProperty("exportPath");
            ObjectMapper objectMapper = new ObjectMapper();
            File inputFile = new File(importPath);
            @Cleanup
            FileInputStream fileInputStream = new FileInputStream(inputFile);

            Workbook workbook = WorkbookFactory.create(fileInputStream);
            Sheet sheet = workbook.getSheetAt(0);
            String jsonStr = "{\n" +
                    "  \"api\": {\n" +
                    "    \"apiLabels\": [\n" +
                    "      {\n" +
                    "        \"apiLabelId\": 3 \n" +
                    "      }\n" +
                    "    ],\n" +
                    "    \"apiTypes\": [\n" +
                    "      {\n" +
                    "        \"apiTypeId\": *apiTypeId* \n" +
                    "      }\n" +
                    "    ],\n" +
                    "    \"appId\": *appId*, \n" +
                    "    \"bodyDescribe\": \"*bodyDescribe*\", \n" +
                    "    \"contactInfo\": [\n" +
                    "      {\n" +
                    "        \"contactName\": \"老杜\",\n" +
                    "        \"contactPhone\": \"18867100000\"\n" +
                    "      }\n" +
                    "    ],\n" +
                    "    \"contentTypeValue\": \"application/json;charset=UTF-8\", \n" +
                    "    \"name\": \"*name*\", \n" +
                    "    \"exposePath\": \"*exposePath*\", \n" +
                    "    \"protocol\": \"HSF\",\n" +
                    "    \"publishUnitCode\": \"5\", \n" +
                    "    \"publishUnitName\": \"通达海\",\n" +
                    "    \"remark\": \"*remark*\",\n" +
                    "    \"requestMethod\": \"POST\",\n" +
                    "    \"returnDataFormat\": \"JSON\",\n" +
                    "    \"userAccount\": \"18322475126\", \n" +
                    "    \"userName\": \"杜长生\"\n" +
                    "  },\n" +
                    "  \"apiErrorCode\": [\n" +
                    "    {\n" +
                    "      \"msg\": \"内部错误\",\n" +
                    "      \"name\": \"500\",\n" +
                    "      \"remark\": \"内部错误\"\n" +
                    "    }\n" +
                    "  ],\n" +
                    "  \"apiFlowControl\": {\n" +
                    "    \"apiLimit\": 10000,\n" +
                    "    \"appLimit\": 10000,\n" +
                    "    \"timeout\": 30,\n" +
                    "    \"unit\": \"SECOND\"\n" +
                    "  },\n" +
                    "  \"hsfInfo\": {\n" +
                    "    \"interfaceName\": \"*interfaceName*\",\n" +
                    "    \"group\": \"HSF\",\n" +
                    "    \"version\": \"1.0.0\",\n" +
                    "    \"methodName\": \"*methodName*\",\n" +
                    "    \"namespaceId\": 2\n" +
                    "  },\n" +
                    "  \"apiParam\":[{\"location\":\"QUERY\",\"isMust\":\"1\",\"name\":\"*dtoname*\",\"type\":\"*dtotype*\",\"defaultValue\":\"obejct\",\"exampleValue\":\"*exampleValue*\",\"remark\":\"入参\",\"definedType\":\"REQUEST\",\"matchType\":0}],\n" +
                    "  \"apiResponseInfo\": {\n" +
                    "    \"failResponseExample\": \"{}\",\n" +
                    "    \"responseExample\": \"{}\",\n" +
                    "    \"responseType\": \"JSON\"\n" +
                    "  }\n" +
                    "}\n";
            // 遍历每一行
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                Map<String, Object> dataMap = new HashMap<>();
                String mkmc = row.getCell(4).getStringCellValue();
                String apiTypeId = "11";
                String appId = "14";
                if ("审判".equals(mkmc)) {
                    apiTypeId = "11";
                    appId = "14";
                } else if ("四类案件".equals(mkmc)) {
                    apiTypeId = "11";
                    appId = "15";
                } else if ("程序性文书模块".equals(mkmc)) {
                    apiTypeId = "16";
                    appId = "17";
                } else if ("通用模块".equals(mkmc)) {
                    apiTypeId = "16";
                    appId = "18";
                } else if ("材料模块".equals(mkmc)) {
                    apiTypeId = "16";
                    appId = "16";
                } else if ("案件空间".equals(mkmc)) {
                    apiTypeId = "10";
                    appId = "9";
                }else if ("档案".equals(mkmc)) {
                    apiTypeId = "16";
                    appId = "1000139";
                }
                String name = row.getCell(6).getStringCellValue();
                String remark = row.getCell(5).getStringCellValue();

                String interfaceName = row.getCell(7).getStringCellValue();
                String methodName = row.getCell(8).getStringCellValue();

                String dtoname = row.getCell(10).getStringCellValue();
                String dtotype = row.getCell(9).getStringCellValue();
                String exampleValue = row.getCell(11).getStringCellValue();
                String exposePath = row.getCell(12).getStringCellValue();
                String bodyDescribe = exampleValue.substring(exampleValue.indexOf(":")+1,exampleValue.length()-1);

                String jsonStrNew = jsonStr.replace("*apiTypeId*", apiTypeId).replace("*appId*", appId)
                        .replace("*name*", name).replace("*remark*", remark)
                        .replace("*interfaceName*", interfaceName).replace("*methodName*", methodName)
                        .replace("*dtoname*", dtoname).replace("*dtotype*", dtotype).replace("*exposePath*", exposePath)
                        .replace("*exampleValue*", exampleValue).replace("*bodyDescribe*", bodyDescribe);

                System.out.println(jsonStrNew);
                File outputFile = new File(exportPath + File.separator + i + "-" + methodName + ".json");
                @Cleanup
                FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
                fileOutputStream.write(jsonStrNew.getBytes());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
